<template>
  <view :class="['job-list-page', currentTheme]" @scroll="onScroll" style="height: 100vh; overflow-y: auto; padding-bottom: 140rpx;">
    <custom-tab-bar />
    <!-- 移除重复的职位列表代码 -->
   <JobList 
  :job-list="jobList" 
  :loading-more="loadingMore" 
  :no-more="noMore"
  :show-delete="true"
  @goToJobDetail="goToJobDetail"
  @deleteJob="deleteJob"
/>
    <!-- 空状态（无数据时） -->
    <view v-if="!isLoading && !errorMsg && jobList.length === 0" class="empty-container">
      <uni-icons type="info" size="48" color="#b6ccd8"></uni-icons>
      <text class="empty-text">暂无相关职位</text>
    </view>
  </view>
</template>

<script>
import {  getEmploymentJobPosting, deletejobPostings } from '@/api/job.js';
import JobList from '@/components/JobList.vue';

export default {
  components: {
    JobList
  },
  data() {
    return {
      pageSize: 4,
      pageNum: 1,
      jobList: [],
      total: 0, // 总记录数
      isLoading: false,
      loadingMore: false,
      noMore: false,
      errorMsg: '',
      currentTheme: 'theme1' // 添加主题数据
    };
  },
  
  methods: {
    // 添加滚动事件处理
    onScroll(e) {
      // 防抖处理，避免频繁触发
      clearTimeout(this.scrollTimer);
      this.scrollTimer = setTimeout(() => {
        const scrollTop = e.detail.scrollTop;
        const scrollHeight = e.detail.scrollHeight;
        const clientHeight = e.detail.clientHeight;
        
        // 当滚动到底部时加载更多
        if (scrollTop + clientHeight >= scrollHeight - 20 && !this.loadingMore && !this.noMore) {
          this.pageNum++;
          this.fetchJobs(true);
        }
      }, 200);
    },
    
    // 修改 fetchJobs 方法，确保正确判断是否还有更多数据
    async fetchJobs(isLoadMore = false) {
      if (this.loadingMore || (isLoadMore && this.noMore)) {
        console.log('阻止重复加载或已无更多数据');
        return;
      }

      this.loadingMore = isLoadMore;
      this.isLoading = !isLoadMore;
      this.errorMsg = '';

      try {
        // 确保参数顺序与API定义一致
        const res = await getEmploymentJobPosting(this.pageNum, this.pageSize);
        
        const data = res.data || {};
        const currentPageData = data.list || [];
        this.total = data.total || 0;

        if (isLoadMore) {
          this.jobList = [...this.jobList, ...currentPageData];
        } else {
          this.jobList = currentPageData;
        }

        // 修复判断逻辑：当已加载数据数量大于等于总数量时，才设置 noMore 为 true
        this.noMore = this.jobList.length >= this.total;
        
        // 如果返回的数据少于 pageSize，也说明没有更多数据了
        if (currentPageData.length < this.pageSize) {
          this.noMore = true;
        }
      } catch (error) {
        console.error('加载职位失败：', error);
        this.errorMsg = '加载失败，请检查网络后重试';
        if (isLoadMore) this.pageNum -= 1;
      } finally {
        this.isLoading = false;
        this.loadingMore = false;
      }
    },

    // 删除帖子
    async deleteJob(id) {
      const res = await deletejobPostings(id);
      if (res.code === 200) {
        uni.showToast({
          title: "删除成功",
          icon: "success",
          duration: 2000
        });
        this.fetchJobs(); 
      } else {
        uni.showToast({
          title: "删除失败，请检查网络问题，稍后重试",
          icon: "none",
          duration: 2000
        });
      }
    },

    goToJobDetail(id) {
      uni.navigateTo({ url: `/employPages/jobRecommendation/jobDetail/jobDetail?id=${id}` });
    },

    reload() {
      console.log('重新加载数据');
      this.pageNum = 1;
      this.noMore = false;
      this.fetchJobs();
    }
  },

  mounted() {
    console.log('页面加载，开始首次加载数据');
    // 初始化滚动定时器
    this.scrollTimer = null;
    // 加载保存的主题
    const savedTheme = uni.getStorageSync('currentTheme');
    if (savedTheme) {
      this.currentTheme = savedTheme;
    }
    this.fetchJobs();
  },

  onShow() {
    // 每次进入页面时重新加载数据
    this.fetchJobs();
    // 重新加载主题设置
    const savedTheme = uni.getStorageSync('currentTheme');
    if (savedTheme) {
      this.currentTheme = savedTheme;
    }
  }
};
</script>


<style scoped>
.job-list-page {
  background-color: #f8f8f8;
  min-height: 100vh;
}

/* 加载状态样式 */
.loading {
  text-align: center;
  padding: 30px 0;
  color: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

/* 错误状态样式 */
.error-state {
  text-align: center;
  padding: 30px 0;
  color: #f53f3f;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
}

.error-state button {
  background-color: #007aff;
  color: white;
  padding: 8px 20px;
  border-radius: 4px;
  font-size: 14px;
}

/* 职位列表样式 */
.job-items {
  padding: 0 10px;
  /* 确保列表高度足够触发滚动 */
  min-height: calc(100vh - 100px);
}

.job-item {
  padding: 15px;
  background-color: white;
  border-radius: 8px;
  margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.job-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.job-name {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.salary {
  color: #409eff;
  font-size: 16px;
  font-weight: bold;
}

.company-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
  gap: 10px;
}

.company-info text:first-child {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 10px;
  gap: 8px;
}

.tag {
  padding: 2px 8px;
  background-color: #f5f5f5;
  border-radius: 4px;
  font-size: 12px;
  color: #666;
}

.contact {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #999;
  font-size: 12px;
}

.contact-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hr-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background-color: #eee;
}

/* 加载更多提示 */
.load-more {
  text-align: center;
  padding: 15px 0;
  color: #999;
  font-size: 14px;
}

/* 空状态样式 */
.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 80px 0;
  color: #999;
}

.empty-text {
  margin-top: 15px;
  font-size: 16px;
}

/* 修改滚动容器样式 */
.job-list-page {
  height: 100vh;
  overflow-y: auto;
  padding-bottom: 80rpx; /* 添加底部间距，避免被导航栏遮挡 */
}

/* 禁用所有动画和过渡效果 */
* {
  transition: none !important;
  animation: none !important;
}

/* Theme1 样式 (蓝色主题) */
.theme1 .job-list-page {
  background-color: #F8FBFF;
}

.theme1 .job-item {
  background-color: #FFFFFF;
  border: 1px solid #E8F4FD;
  box-shadow: 0 4rpx 16rpx rgba(74, 144, 226, 0.08);
}

.theme1 .job-item:hover {
  border-color: #4A90E2;
  box-shadow: 0 6rpx 20rpx rgba(74, 144, 226, 0.15);
}

.theme1 .job-name {
  color: #2C3E50;
  font-weight: 600;
}

.theme1 .salary {
  color: #4A90E2;
  font-weight: 600;
}

.theme1 .company-info {
  color: #5A6C7D;
}

.theme1 .tag {
  background-color: #E8F4FD;
  color: #4A90E2;
  border: 1px solid #D1E9F6;
}

.theme1 .contact {
  color: #5A6C7D;
}

.theme1 .empty-container {
  color: #5A6C7D;
}

.theme1 .loading {
  color: #4A90E2;
}

.theme1 .error-state {
  color: #e74c3c;
}

.theme1 .error-state button {
  background-color: #4A90E2;
  color: white;
  box-shadow: 0 4rpx 16rpx rgba(74, 144, 226, 0.15);
}

/* Theme2 样式 (棕色主题) */
.theme2 .job-list-page {
  background-color: #FDF8F3;
}

.theme2 .job-item {
  background-color: #FFFBF7;
  border: 1px solid #F5E6D3;
  box-shadow: 0 4rpx 16rpx rgba(160, 82, 45, 0.08);
}

.theme2 .job-item:hover {
  border-color: #A0522D;
  box-shadow: 0 6rpx 20rpx rgba(160, 82, 45, 0.15);
}

.theme2 .job-name {
  color: #3E2723;
  font-weight: 600;
}

.theme2 .salary {
  color: #A0522D;
  font-weight: 600;
}

.theme2 .company-info {
  color: #6D4C41;
}

.theme2 .tag {
  background-color: #F5E6D3;
  color: #A0522D;
  border: 1px solid #E8D5C4;
}

.theme2 .contact {
  color: #6D4C41;
}

.theme2 .empty-container {
  color: #6D4C41;
}

.theme2 .loading {
  color: #A0522D;
}

.theme2 .error-state {
  color: #cd853f;
}

.theme2 .error-state button {
  background-color: #A0522D;
  color: white;
  box-shadow: 0 4rpx 16rpx rgba(160, 82, 45, 0.15);
}
</style>